/*
 * Copyright 2012 Alibaba.com All right reserved. This software is the
 * confidential and proprietary information of Alibaba.com ("Confidential
 * Information"). You shall not disclose such Confidential Information and shall
 * use it only in accordance with the terms of the license agreement you entered
 * into with Alibaba.com.
 */
package com.dj.web.form.tools;

import net.sf.cglib.beans.BeanCopier;

import org.springframework.util.StringUtils;

import com.dj.domain.entity.DaiJiaProfile;
import com.dj.domain.entity.DaiJiaUser;
import com.dj.web.entity.DaijiaWebUser;
import com.dj.web.reg.controller.BasicForm;
import com.dj.web.reg.controller.FullProfileForm;
import com.dj.web.reg.controller.RegUserForm;

/**
 * 类UserFormCopier.java的实现描述：TODO 类实现描述
 * 
 * @author jiansheng.xujs 2012-6-4 下午1:32:21
 */
public class UserFormCopier {

    private static BeanCopier regUserForm2userDo    = BeanCopier.create(RegUserForm.class, DaiJiaUser.class, false);

    private static BeanCopier profileForm2ProfileDo = BeanCopier.create(FullProfileForm.class, DaiJiaProfile.class,
                                                                        false);

    private static BeanCopier profileDo2profileForm = BeanCopier.create(DaiJiaProfile.class, FullProfileForm.class,
                                                                        false);

    private static BeanCopier userBasicForm2Do      = BeanCopier.create(BasicForm.class, DaiJiaUser.class, false);

    public static DaiJiaUser convertRegUserForm2DaijiaUser(RegUserForm regUserForm, DaiJiaUser userDo) {
        if (regUserForm == null) return userDo;
        if (userDo == null) userDo = new DaiJiaUser();
        regUserForm2userDo.copy(regUserForm, userDo, null);
        userDo.setSite(DaiJiaUser.DEFAULT_USER_SITE);
        return userDo;
    }

    public static DaiJiaProfile convertProfileForm2DaijiaProfile(DaijiaWebUser webUser, FullProfileForm profileForm,
                                                                 DaiJiaProfile profile) {
        if (profileForm == null) return profile;
        if (profile == null) profile = new DaiJiaProfile();
        profileForm2ProfileDo.copy(profileForm, profile, null);
        if (!StringUtils.hasLength(profileForm.getSex())) {
            profile.setSex(DaiJiaProfile.SEX_UNKNOW);
        }
        profile.setLevel(DaiJiaProfile.LEVEL_NOT_AUTH);
        if (webUser != null) {
            profile.setSource(DaiJiaProfile.SOURCE_REG);
            profile.setUid(webUser.getId());
        } else {
            profile.setSource(DaiJiaProfile.SOURCE_NET);
        }
        return profile;
    }

    public static DaiJiaUser convertDaijiaUserBasicForm2Do(BasicForm form, DaiJiaUser user, DaijiaWebUser webUser) {
        if (form == null) return user;
        if (user == null) user = new DaiJiaUser();
        userBasicForm2Do.copy(form, user, null);
        user.setId(webUser.getId());
        return user;
    }

    public static FullProfileForm convertDaijiaProfile2ProfileForm(DaijiaWebUser webUser, DaiJiaProfile profileDo,
                                                                   FullProfileForm fullProfileForm) {
        if (profileDo == null) return fullProfileForm;
        if (fullProfileForm == null) fullProfileForm = new FullProfileForm();
        profileDo2profileForm.copy(profileDo, fullProfileForm, null);
        if (webUser != null) {
            // 无省市区信息
            if (!StringUtils.hasLength(fullProfileForm.getProvince())) {
                fullProfileForm.setProvince(webUser.getProvince());
                fullProfileForm.setCity(webUser.getCity());
                fullProfileForm.setCounty(webUser.getCounty());
            }
            if (!StringUtils.hasLength(fullProfileForm.getDrivingLicense())) {
                fullProfileForm.setDrivingLicense("C1");
            }
        }
        if (!StringUtils.hasLength(profileDo.getSex())) {
            if (webUser != null && StringUtils.hasLength(webUser.getSex())) {
                fullProfileForm.setSex(webUser.getSex());
            } else {
                fullProfileForm.setSex(DaiJiaProfile.SEX_UNKNOW);
            }
        }
        return fullProfileForm;
    }
}
